package demo1;

public class Test {
    public static int countSegments(String s) {
        if(s.length() == 0){
            return 0;
        }
        String[] strings = s.split(" ");
        int sum = 0;
        for(int i = 0;i < strings.length; i++){
            if(strings[i].length() != 0) {
                if (strings[i].charAt(0) >= 'A' && strings[i].charAt(0) <= 'Z' ||
                        strings[i].charAt(0) >= 'a' && strings[i].charAt(0) <= 'z') {
                    sum++;
                }
            }
        }
        return sum;
    }
    public static void main(String[] args) {
        String s = ", , , ,        a, eaefa";
        int count = countSegments(s);
        System.out.println(count);
    }
    public static void main12(String[] args) {
        long start = System.currentTimeMillis();
        String s = "";
        for(int i = 0; i < 10000; ++i){
                s += i;
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);

        start = System.currentTimeMillis();
        StringBuffer sbf = new StringBuffer("");
        for(int i = 0; i < 10000; ++i){
                sbf.append(i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);

        start = System.currentTimeMillis();
        StringBuilder sbd = new StringBuilder();
        for(int i = 0; i < 10000; ++i){
            sbd.append(i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);
    }
    public static void main11(String[] args) {
        String str = "    hello  world  and you   ";
        String str2 = str.trim();
        System.out.println(str2);
    }
    public static void main10(String[] args) {
        String str = "hello world";
        String str2 = str.substring(3,7);
        System.out.println(str2);
    }
    public static void main9(String[] args) {
        String str = "192.168.1.1";
        String[] str2 = str.split("\\.");
        for (int i = 0; i < str2.length; i++) {
            System.out.println(str2[i]);
        }

        System.out.println("=====================");

        String str3 = "name=zhangsan&age=18" ;
        String[] result = str3.split("=|&") ;
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }

        System.out.println("=========================");

        String str4 = "name=lisi+age=20";
        String[] strings3 = str4.split("\\+");
        for (int i = 0; i < strings3.length; i++) {
            System.out.println(strings3[i]);
        }
        System.out.println("============================");

        String str5 = "hello\\world\\and\\you";
        String[] result2 = str5.split("\\\\");
        for (int i = 0; i < result2.length; i++) {
            System.out.println(result2[i]);
        }

    }
    public static void main8(String[] args) {
        String str = "hello world and you";
        String[] str2 = str.split(" ",3);
        for (int i = 0; i < str2.length; i++) {
            System.out.println(str2[i]);
        }
    }
    public static void main7(String[] args) {
        String str = "hello world hello bit" ;
        String[] result = str.split(" ",2) ;
        for(String s: result) {
            System.out.println(s);
        }
    }
    public static void main6(String[] args) {
        String str1 = "hello world";
        String[] strings = str1.split("l");

        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }

        System.out.println("===========================");

        String[] strings2 = str1.split("ll");
        for (int i = 0; i < strings2.length; i++) {
            System.out.println(strings2[i]);
        }
    }
    public static void main5(String[] args) {
        String str1 = "hellQllo";
        String str2 = str1.replaceAll("l","P");
        System.out.println(str2);

        String str3 = str1.replaceAll("ll","SS");
        System.out.println(str3);
    }
    public static void main4(String[] args) {
        String str1 = "helloQlloQllo";
        String str2 = str1.replace("llo","AA");
        System.out.println(str2);
    }
    public static void main3(String[] args) {
        String str1 = "hello";
        String str2 = str1.replace('l','P');
        System.out.println(str2);
    }

    public static void main2(String[] args) {
        String str = String.format("%d-%d-%d",2024,6,23);
        System.out.println(str);

        String str2 = String.format("%.2f-%.2f-%.2f",3.14,2.73,1024.16);
        System.out.println(str2);
    }

    public static void main1(String[] args) {
        String str = "hello";
        char[] arr = str.toCharArray();

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
